package com.neusoft.nepb.common;

import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;

public class SceneUtils {
    public static void switchTo(ActionEvent event, String fxmlPath) throws IOException {
        URL resourceUrl = SceneUtils.class.getResource(fxmlPath);
        if (resourceUrl == null) {
            throw new IOException("无法找到FXML资源: " + fxmlPath);
        }

        FXMLLoader fxmlLoader = new FXMLLoader(resourceUrl);
        Parent root = fxmlLoader.load();

        // 获取当前窗口的 Stage
        Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();

        // 创建新的 Scene 并设置到 Stage 上
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

    public static void setImage(ImageView imageView,String file){
        //初始化图像
        Image image = new Image(file);
        imageView.setImage(image);
        imageView.setPreserveRatio(false);
        imageView.setSmooth(true);
    }
}
